<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\WorkContact\Action\Workbench;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\WorkbenchAuthMiddleware;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\App\WorkContact\Contract\WorkContactEmployeeContract;
use MoChat\App\WorkContact\Contract\WorkContactTagPivotContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;

/**
 * 首页-打标签-获取无标签客户.
 * @Controller
 */
class NoTagContact extends AbstractAction
{
    /**
     * @Inject
     * @var WorkContactEmployeeContract
     */
    protected $workContactEmployeeService;

    /**
     * @Inject
     * @var WorkContactContract
     */
    protected $workContactContract;

    /**
     * @Inject
     * @var WorkContactTagPivotContract
     */
    protected $workContactTagPivotService;

    /**
     * @Middlewares({
     *     @Middleware(WorkbenchAuthMiddleware::class)
     * })
     * @RequestMapping(path="/workbench/workContact/noTagContact", methods="GET")
     * @return array 返回数组
     */
    public function handle(): array
    {
        $user = user();
        // 判断用户绑定企业信息
        if (! isset($user['corpId'])) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        $corpId = $user['corpId'];
        $employeeId = $user['id'];
//        $corpId = 1;//测试
//        $employeeId = 5;//测试

        // 查询员工所有客户
        $contacts = $this->workContactEmployeeService->getWorkContactEmployeeByCorpIdEmployeeId($employeeId, $corpId, ['contact_id', 'create_time']);
        if (empty($contacts)) {
            return [];
        }
        $contactIds = array_unique(array_column($contacts, 'contactId'));
        // 查询已打标签客户
        $contactTags = $this->workContactTagPivotService->getWorkContactTagPivotsByContactIds($contactIds, ['contact_id']);
        if (! empty($contactTags)) {
            $contactTagIds = array_unique(array_column($contactTags, 'contactId'));
            $contactIds = array_diff($contactIds, $contactTagIds);
        }
        if (empty($contactIds)) {
            return [];
        }
        $contacts = array_column($contacts, null, 'contactId');
        // 查询客户信息
        $list = $this->workContactContract->getWorkContactsById($contactIds, ['id', 'name', 'avatar', 'wx_external_userid']);
        foreach ($list as &$item) {
            $item['avatar'] = file_full_url($item['avatar']);
            $item['createTime'] = $contacts[$item['id']] ? $contacts[$item['id']]['createTime'] : '';
        }
        unset($item);
        return ['count' => count($list), 'list' => $list];
    }
}
